<!-- 查询库存 -->
<template>
    <div style="padding: 0;border: none;">
        <el-button :disabled="disabled" v-bind="$attrs" @click="handleQueryStock">
            查询库存
        </el-button>

        <el-dialog draggable v-model="dialogTableVisible" title="查询现有库存可用量" width="75%">
            <p class="tip">{{ tip }}</p>
            <el-table :data="gridData" max-height="400">
                <el-table-column property="factoryCode" label="工厂" min-width="100" show-overflow-tooltip/>
                <el-table-column property="inventoryAdd" label="库位" min-width="100" show-overflow-tooltip/>
                <el-table-column property="inventoryDesc" label="库位描述" min-width="150" show-overflow-tooltip/>
                <el-table-column property="inventoryTotalNum" label="当前库存总量" min-width="150"
                                 show-overflow-tooltip/>
                <el-table-column property="availableInventoryNum" label="当前可用库存总量" min-width="150"
                                 show-overflow-tooltip/>
                <el-table-column property="materialDesc" label="物料描述" min-width="180" show-overflow-tooltip/>
                <el-table-column property="materialCode" label="物料编码" min-width="160" show-overflow-tooltip/>
            </el-table>
        </el-dialog>
    </div>
</template>

<script setup>
import {getCurrentInstance, ref} from 'vue'
import {doGet, doPost, doPut, doDelete, doDownload} from '@/config/http'
import {echoMaterialdes} from "@/utils/echoDescription";

const {proxy} = getCurrentInstance();
const props = defineProps({
    params: {
        type: Array,
        default: () => []
    },
    disabled: {
        type: Boolean,
        default: false
    },
    tipShow: {
        type: Boolean,
        default: false
    },
});

const dialogTableVisible = ref(false)
const tip = ref('当前可用库存总量包含采购在途数量')
const svc = ref('peas-module-plan-biz')

const gridData = ref([])

function handleQueryStock() {
    let {params} = props
    if (!params.length) {
        proxy.$alert('请选择需要查询库存信息的记录', "提示", {type: 'warning'})
        return false
    }
    let purFactory = params[0]?.purFactory
    let isOne = params.every(item => item.purFactory === purFactory)
    if (!isOne) {
        proxy.$alert('请选择相同工厂的记录', "提示", {type: 'warning'})
        return false
    }
    let availableStockRuleCode = params[0]?.availableStockRuleCode
    let queryParams = {
        availableStockRuleCode,
        purFactory,
        items: params.map(item => {
            return {
                materialCode: item.materialCode,
                purInventoryAdd: item.purInventoryAdd
            }
        })
    }
    if (availableStockRuleCode === 'Z1') {
        tip.value = '当前可用库存总量不包含采购在途数量'
    }
    doPost(svc.value, '/transfer/query-stock-list', queryParams,true).then(res => {
        gridData.value =  res.filter(item => item.inventoryAdd)
        proxy.$emit('getStockList', res)
        echoMaterialdes(gridData.value, ['materialCode'], ['materialDesc'])
        dialogTableVisible.value = true
    }).catch(e => {
        console.error(e)
    })
}

</script>
<style scoped>
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.tip {
    position: absolute;
    left: 20px;
    top: 50px;
    color: var(--el-color-danger);
}
</style>
